package data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import domain.LineaPedido;
import domain.Pedido;
import domain.Producto;

public class JDBCLineaPedidoDAO implements ILineaPedidoDAO{
	IProductoDAO pdao;
    
    public JDBCLineaPedidoDAO() {
        pdao = new JDBCProductoDAO();
    }
	public void delete(Connection conn, String pedidoOID) {
		// TODO Auto-generated method stub
		
	}

	public void insert(Connection conn, List<LineaPedido> lineasPedidos, String pedidoOID) {
		// TODO Auto-generated method stub
		
	}

	public List<LineaPedido> select(Connection conn, String pedidoOID) {
		    List lineaPedidos = new LinkedList();
	        String sql = "SELECT * FROM Detail WHERE (pedidoOID = ? ) ";
	        PreparedStatement stmt = null;
	        ResultSet result = null;

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, pedidoOID);

	            result = stmt.executeQuery();

	            while (result.next()) {
	                LineaPedido lp = new LineaPedido();
	                lp.setCantidad(result.getInt("cantidad"));
	                Producto p = pdao.select(conn, result.getString("productoOID"));
	                lp.setProducto(p);
	                lineaPedidos.add(lp);
	            }
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }

	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }

	        return lineaPedidos;
	    }
	}

